package cn.jiangjiesheng.bootstrap.commons.response;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Getter;

import java.io.Serializable;

/**
 * Created by dev@jiangjiesheng.cn on 2021/8/25  <br/>
 *
 * @author jerrylau
 */
@Getter
@Builder
public class ResponseWrapper<T> implements Serializable {

    @ApiModelProperty(value = "请求成功：true，请求处理失败：false")
    private boolean success;

    @ApiModelProperty(value = "请求成功：success， 请求失败：返回异常信息")
    private String message;

    @ApiModelProperty(value = "请求成功时，返回结果；请求失败时返回 null")
    private T result;

    @ApiModelProperty(value = "异常信息")
    private String trace;

    public String toJson(){
        try {
            return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String toString(){
        return toJson();
    }
}
